/*-
 * HuffNode.java
 *
 * $LastChangedBy: stein $
 * $LastChangedRevision: 95 $
 * $LastChangedDate: 2007-02-13 16:19:24 +0000 (Tue, 13 Feb 2007) $
 * $HeadURL: svn://localhost/SMS/src/com/googlecode/attention/sms/comp/HuffNode.java $
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.sms.comp;

/** * Node in the Huffman coding. *  * @author Stein Gunnar Bakkeby */
public class HuffNode {
  /* Field values */
  private int  weight; // probability of symb occurring
  private int symbol; // the symbol to be encoded
  private HuffNode left = null;
  private HuffNode right = null;

  /**
   * Constructor, creates an empty node.
   */
  public HuffNode() {
    left   = null;
    right  = null;
    symbol = 0;
    weight = 0;
  }
  
  /**
   * Constructor, creates a node with a given symbol and weight.
   * 
   * @param symbol The symbol that this node represents
   * @param weight The weight of the symbol
   */
  public HuffNode(final int symbol, final int weight) {
    left  = null;
    right = null;
    this.symbol = symbol;
    this.weight = weight;
  }

  /**
   * This will set the right node.
   * 
   * @param right The right node
   */
  public void setRight(final HuffNode right) {
    this.right = right;
  }

  /**
   * This will set the left node.
   * 
   * @param left The left node
   */
  public void setLeft(final HuffNode left) {
    this.left = left;
  }

  /**
   * This will set the symbol.
   * 
   * @param symbol The symbol
   */
  public void setSymbol(final int symbol) {
    this.symbol = symbol;
  }

  /**
   * This will set the weight of the symbol.
   * 
   * @param weight The weight
   */
  public void setWeight(final int weight) {
    this.weight = weight;
  }

  /**
   * This will return the left node.
   * 
   * @return The left node, or null if not present
   */
  public HuffNode getLeft() {
    return left;
  }

  /**
   * This will return the right node.
   * 
   * @return The right node, or null if not present
   */
  public HuffNode getRight() {
    return right;
  }

  /**
   * This will return the symbol.
   * 
   * @return The symbol
   */
  public int getSymbol() {
    return symbol;
  }

  /**
   * This will return the weight of the symbol.
   * 
   * @return The weight of the symbol
   */
  public int getWeight() {
    return weight;
  }

  
  /**
   * Compares a HuffNode weight to another, note that this is 
   * different from .equals()
   * 
   * @param another Another HuffNode
   * @return 0 if equal weight, 1 if this > another, -1 if another > this
   */
  public int compareTo(final HuffNode another) {
    int a_weight = another.getWeight();
         if (a_weight == weight) return 0;
    else if (a_weight  > weight) return -1;
    else return 1;
  }
  
  /**
   * Returns true if the node is a leaf (e.g. no children).
   * 
   * @return True if leaf, false otherwise
   */
  public boolean isLeaf() {
    return left == null && right == null;
  }
}